package com.example.teacher.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.teacher.ExaminStatusSocket;
import com.example.teacher.R;
import com.example.teacher.UUIDUtil;
import com.example.teacher.bean.BasicResponse;
import com.example.teacher.bean.LoginBean;
import com.example.teacher.bean.TestCenterListBean;
import com.example.teacher.network.DefaultObserver;
import com.example.teacher.network.NetWorkManager;
import com.orhanobut.hawk.Hawk;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class TeacherLoginActivity extends RxAppCompatActivity implements ExaminStatusSocket.Callback {

    private  List<TestCenterListBean>  listBeanList=new ArrayList<>();
    private ArrayAdapter<String> adapter;
    private Spinner spinner;
    private List<String> name=new ArrayList<>();
    private EditText test_id;
    private TextView login_btn;
    private LoginBean  loginBean;
    private String examRoom="";
    private String examRoomCode="";
    private String  userId;
    final int ALL_PERMISSIONS_CODE = 1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher_login);
        spinner = (Spinner) findViewById(R.id.spinner);
        test_id=(EditText)findViewById(R.id.test_id);
        login_btn =(TextView)findViewById(R.id.login_btn);
        ExaminStatusSocket.get().init(this);
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED|| ContextCompat.checkSelfPermission(this, Manifest.permission.MODIFY_AUDIO_SETTINGS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO,Manifest.permission.MODIFY_AUDIO_SETTINGS}, ALL_PERMISSIONS_CODE);
        }
        //获取考场
     //   getData();
        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               //登入
                logIn();
            }
        });
        name.clear();
        name.add("请选择考场");
        TestCenterListBean testCenterListBean1=new TestCenterListBean();
        testCenterListBean1.setRoomName("考场1");
        testCenterListBean1.setRoomCode("1");
        TestCenterListBean testCenterListBean2=new TestCenterListBean();
        testCenterListBean2.setRoomName("考场2");
        testCenterListBean2.setRoomCode("2");
        listBeanList.add(testCenterListBean1);
        listBeanList.add(testCenterListBean2);
        for (int i=0;i<listBeanList.size();i++){
            name.add(listBeanList.get(i).getRoomName());
        }
        setData(name);
    }
   private void getData(){

     NetWorkManager.getInstance().getNetWorkApiService().getRoomList().compose(this.<BasicResponse<List<TestCenterListBean>>>bindToLifecycle())
               .subscribeOn(Schedulers.io())
               .observeOn(AndroidSchedulers.mainThread())
               .subscribe(new DefaultObserver<BasicResponse<List<TestCenterListBean>>>(){
                   @Override
                   protected void onSuccess(BasicResponse<List<TestCenterListBean>> response) {

                       if (response != null) {
                           if (response.getData() != null) {
                               listBeanList = (List<TestCenterListBean>) response.getData();
                               name.clear();
                               name.add("请选择考场");
                              for (int i=0;i<listBeanList.size();i++){
                                  name.add(listBeanList.get(i).getRoomName());
                              }
                               setData(name);
                           }

                       }
                   }

                   @Override
                   protected void onFail(BasicResponse<List<TestCenterListBean>> response) {
                       super.onFail(response);
                   }
               });




   }
   private  void  setData(List<String>stringList){

       //将可选内容与ArrayAdapter连接起来，simple_spinner_item是android系统自带样式
       adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,name);
       //设置下拉列表的风格,simple_spinner_dropdown_item是android系统自带的样式，等会自定义修改
       adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       //将adapter 添加到spinner中
       spinner.setAdapter(adapter);
       //添加事件Spinner事件监
       spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
           @Override
           public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
               examRoom=adapterView.getItemAtPosition(i).toString();//获取i所在的文本
               Log.d("fsdgg",examRoomCode+"   "+examRoom);
           }

           @Override
           public void onNothingSelected(AdapterView<?> adapterView) {

           }
       });



   }
   //登入
   private void logIn(){
       for(int k=0;k<listBeanList.size();k++){
           if (examRoom.equals(listBeanList.get(k).getRoomName())){
               //获取考场code值 ；登入的入参
               examRoomCode=listBeanList.get(k).getRoomCode();
               Hawk.put("examRoomCode",examRoomCode);
               //type 1;考生  0；考官 进入监考界面
               Intent intent=new Intent(TeacherLoginActivity.this,MainActivity2.class);
               startActivity(intent);
               finish();
               Log.d("fsdgg111",examRoomCode+"   "+examRoom);
           }

       }

 /*   if ("请选择考场".equals(examRoom)||examRoomCode.isEmpty()||examRoom.isEmpty()){
            Toast.makeText(TeacherLoginActivity.this,"请选择考场",Toast.LENGTH_SHORT).show();
            return;
        }
        //随机用户id
       userId= UUIDUtil.get16UUID();
       Hawk.put("userId",userId);
       Hawk.put("examRoomCode",examRoomCode);
       NetWorkManager.getInstance().getNetWorkApiService().logIn(examRoomCode,userId).compose(this.<BasicResponse<LoginBean>>bindToLifecycle())
               .subscribeOn(Schedulers.io())
               .observeOn(AndroidSchedulers.mainThread())
               .subscribe(new DefaultObserver<BasicResponse<LoginBean>>(){
                   @Override
                   protected void onSuccess(BasicResponse<LoginBean> response) {

                       if (response != null) {
                           if (response.getData() != null) {
                               loginBean = (LoginBean) response.getData();
                               Toast.makeText(TeacherLoginActivity.this,"登入成功！",Toast.LENGTH_SHORT).show();
                              //type 1;考生  0；考官 进入监考界面
                               Intent intent=new Intent(TeacherLoginActivity.this,TeacherInvigilatActivity.class);
                               startActivity(intent);
                               finish();

                           }

                       }
                   }

                   @Override
                   protected void onFail(BasicResponse<LoginBean> response) {
                       super.onFail(response);
                   }
               });
*/

   }
    @Override
    public void onStartExam() {

    }

    @Override
    public void onEndExam() {

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
